	RecordStore rs;
	
	void saveGame()
	{		
		byte[] saveData = new byte[SAVE.TOTAL_RMS_SIZE];
		
		saveData[SAVE.TURN] = (byte)cPlayer.turn;
		saveData[SAVE.FIRST_ROUND_TURN] = (byte)cPlayer.firstRoundTurn;
		saveData[SAVE.ROUND_TYPE] = (byte)cPlayer.roundType;
		
		int lastSaveIndex = SAVE.ARMIES_START;
		
		for(int i = 0; i < tegPlayers.length; i++)
		{
			saveData[SAVE.ARMIES_START + i] = (byte)tegPlayers[i].armies;
			lastSaveIndex++;
		}
		
		for(int i = SAVE.ARMIES_END - 1; i > lastSaveIndex; i--)
		{
			saveData[i] = (byte)-1;
		}
		
		lastSaveIndex = SAVE.ARMIES_END;
		
		for(int i = 0; i < tegPlayers.length; i++)
		{
			saveData[SAVE.LAST_CONQ_START + i] = (byte)tegPlayers[i].lastConqueredCountry;
			lastSaveIndex++;
		}
		
		for(int i = SAVE.LAST_CONQ_END - 1; i > lastSaveIndex; i--)
		{
			saveData[i] = (byte)-1;
		}
		
		lastSaveIndex = SAVE.LAST_CONQ_END;
		
		for(int i = 0; i < tegPlayers.length; i++)
		{
			saveData[SAVE.OBJECTIVE_START + i] = (byte)tegPlayers[i].objectiveId;
			lastSaveIndex++;
		}
		
		for(int i = SAVE.OBJECTIVE_END - 1; i > lastSaveIndex; i--)
		{
			saveData[i] = (byte)-1;
		}
		
		lastSaveIndex = SAVE.OBJECTIVE_END;
		
		for(int i = 0; i < tegPlayers.length; i++)
		{
			saveData[SAVE.CHANGE_START + i] = (byte)tegPlayers[i].changeNumber;
			lastSaveIndex++;
		}
		
		for(int i = SAVE.CHANGE_END - 1; i > lastSaveIndex; i--)
		{
			saveData[i] = (byte)-1;
		}
		
		lastSaveIndex = SAVE.CHANGE_END;
		
		for(int i = 0; i < tegPlayers.length; i++)
		{
			saveData[SAVE.IS_NPC_START + i] = (byte)((tegPlayers[i].NPC)? 1 : 0);
			lastSaveIndex++;
		}
		
		for(int i = SAVE.IS_NPC_END - 1; i > lastSaveIndex; i--)
		{
			saveData[i] = (byte)-1;
		}
		
		lastSaveIndex = SAVE.IS_NPC_END;
		byte[] tmp;
		
		for(int i = 0; i < tegPlayers.length; i++)
		{
			tmp = intToByteArray(tegPlayers[i].color, 3);
			for(int j = 0; j < 3; j++)
			{
				saveData[SAVE.PLAYER_COLORS_START + j + 3 * i] = tmp[j];
				lastSaveIndex++;
			}
		}
		
		for(int i = SAVE.PLAYER_COLORS_END; i > lastSaveIndex; i--)
		{
			saveData[i] = (byte)-1;
		}
		
		lastSaveIndex = SAVE.PLAYER_COLORS_END;
		
		for(int i = 0; i < tegPlayers.length; i++)
		{
			tmp = tegPlayers[i].name.getBytes();
			
			for(int j = 0; j < tmp.length; j++)
			{
				saveData[SAVE.PLAYER_NAMES_START + i * 10 + j] = tmp[j];
				lastSaveIndex++;
			}
				
			if(tmp.length < 10)
			{
				for(int j = 0; j < 10 - tmp.length; j++)
				{
					saveData[lastSaveIndex] = (byte)-1;
					lastSaveIndex++;
				}
			}
		}
		
		for(int i = SAVE.PLAYER_NAMES_END - 1; i > lastSaveIndex; i--)
		{
			saveData[i] = (byte)-1;
		}
		
		lastSaveIndex = SAVE.PLAYER_NAMES_END;
		
		for(int i = 0; i < tegCountries.length; i++)
		{
			saveData[SAVE.COUNTRY_OWNER_START + i] = (byte)tegCountries[i].owner;
			lastSaveIndex++;
		}
		
		for(int i = 0; i < tegCountries.length; i++)
		{
			saveData[SAVE.COUNTRY_CARD_OWNER_START + i] = (byte)tegCountries[i].cardOwner;
			lastSaveIndex++;
		}
		
		for(int i = 0; i < tegCountries.length; i++)
		{
			saveData[SAVE.COUNTRY_USED_CARD + i] = ((tegCountries[i].usedCard)? (byte)1 : (byte)0);
			lastSaveIndex++;
		}
		
		for(int i = 0; i < tegCountries.length; i++)
		{
			saveData[SAVE.COUNTRY_ARMIES + i] = (byte)tegCountries[i].armies;
			lastSaveIndex++;
		}
		
		for(int i = 0; i < tegCountries.length; i++)
		{
			saveData[SAVE.COUNTRY_REGROUPABLE_ARMIES + i] = (byte)tegCountries[i].regroupableArmies;
			lastSaveIndex++;
		}
		
		for(int i = 0; i < rules.length; i++)
		{
			saveData[SAVE.RMS_RULES + i] = (byte)rules[i];
			lastSaveIndex++;
		}
		
		saveData[SAVE.HAS_PICKED_CARD] = (byte)((hasPickedCard)? 1 : 0);
		lastSaveIndex++;
		saveData[SAVE.REGROUPING] = (byte)((regrouping)? 1 : 0);
		lastSaveIndex++;
		
		try
		{
			rs = RecordStore.openRecordStore(SAVE.RMS_NAME, true);
			
			if(rs.getNextRecordID() - 1 == 0)
			{
				rs.addRecord(new byte[1], 0, 1);
			}
			
			rs.setRecord(1, saveData, 0, saveData.length);
			
			rs.closeRecordStore();
		}
		catch(Exception e)
		{
			System.out.println("ERROR: " + e);
		}
	}
	
	byte[] intToByteArray(int theInt, int bytes)
	{
		byte[] array = new byte[bytes];
		
		switch(bytes)
		{
			case 4:
				array[3] = (byte)((theInt >> 24) & 0xFF);
			case 3:
				array[2] = (byte)((theInt >> 16) & 0xFF);				
			case 2:
				array[1] = (byte)((theInt >> 8) & 0xFF);
			case 1:
				array[0] = (byte)(theInt & 0xFF);
		}
		
		return array;
	}
	
	int loadingStep = 0;
	byte[] saveData = new byte[SAVE.TOTAL_RMS_SIZE];
	
	void UpdateLoadMatch()
	{
		
		switch(loadingStep)
		{
			case INGAME.LOADING_RMS:
			
				try
				{
					rs = RecordStore.openRecordStore(SAVE.RMS_NAME, false);
					System.arraycopy(rs.getRecord(1), 0, saveData, 0, rs.getRecord(1).length);
					rs.closeRecordStore();
					boardImg = Image.createImage(SPRITES.BOARD_IMAGE);
				}
				catch(Exception e)
				{
					System.out.println("ERROR: " + e.toString());
				}				
				
				loadingStep++;
				
			break;
			
			case INGAME.LOADING_PLAYERS:
			
				cPlayer.turn = (int)saveData[SAVE.TURN];
				cPlayer.firstRoundTurn = (int)saveData[SAVE.FIRST_ROUND_TURN];
				cPlayer.roundType = (int)saveData[SAVE.ROUND_TYPE];
				
				for(int i = 0; i < RULES.TOTAL_RULES; i++)
				{
					rules[i] = (int)saveData[SAVE.RMS_RULES + i];
				}
				
				tegPlayers = null;
				cPlayer.totalPlayers = 0;
				tegPlayers = new cPlayer[rules[RULES.MATCH_PLAYERS]];
				
				for(int i = 0; i < rules[RULES.MATCH_PLAYERS]; i++)
				{
					tegPlayers[i] = new cPlayer();
					tegPlayers[i].armies = (int)saveData[SAVE.ARMIES_START + i];
					tegPlayers[i].lastConqueredCountry = (int)saveData[SAVE.LAST_CONQ_START + i];
					tegPlayers[i].objectiveId = (int)saveData[SAVE.OBJECTIVE_START + i];
					tegPlayers[i].changeNumber = (int)saveData[SAVE.CHANGE_START + i];
					tegPlayers[i].NPC = ((saveData[SAVE.IS_NPC_START + i] == 1)? true : false) ;
					byte[] tmp = new byte[10];
					for(int j = 0; j < tmp.length; j++)
					{
						tmp[j] = saveData[SAVE.PLAYER_NAMES_START + i * MENUS.INPUT_NAME_MAX_CHARS + j];
					}
					tegPlayers[i].name = new String(tmp);
					tegPlayers[i].color = 0;
					for(int j = 0; j < 3 ; j++)
					{
						if(saveData[SAVE.PLAYER_COLORS_START + i * 3 + j] != 0)
							tegPlayers[i].color |= ((-(saveData[SAVE.PLAYER_COLORS_START + i * 3 + j]) + 0x54) << (8 * j));
						else
							tegPlayers[i].color |= ((-(saveData[SAVE.PLAYER_COLORS_START + i * 3 + j])) << (8 * j));
					}
				}
				
				hasPickedCard = ((saveData[SAVE.HAS_PICKED_CARD]==1)? true : false);
				regrouping = ((saveData[SAVE.REGROUPING]==1)? true : false);
				
				loadingStep++;
				
			break;
			
			case INGAME.LOADING_COUNTRIES:
				
				tegCountries = new cCountry[COUNTRY_INFO.TOTAL_COUNTRIES];
				
				for(int i = 0; i < COUNTRY_INFO.TOTAL_COUNTRIES; i++)
				{
					tegCountries[i] = new cCountry(COUNTRY_INFO.NAME[i], COUNTRY_INFO.CONTINENT[i], (int)saveData[SAVE.COUNTRY_OWNER_START + i], COUNTRY_INFO.LIMITS[i], COUNTRY_INFO.RECT[i], COUNTRY_INFO.CARD_IMAGE[i], COUNTRY_INFO.ALASKA + i);
					tegCountries[i].cardOwner = (int)saveData[SAVE.COUNTRY_CARD_OWNER_START + i];
					tegCountries[i].usedCard = ((saveData[SAVE.COUNTRY_USED_CARD + i]==1)? true : false);
					tegCountries[i].armies = (int)saveData[SAVE.COUNTRY_ARMIES + i];
					tegCountries[i].regroupableArmies = (int)saveData[SAVE.COUNTRY_REGROUPABLE_ARMIES + i];
				}
				
				int availableCards = 0;
		
				int card = 0;
				boolean cardInStack;
				
				for(int j = 0; j < COUNTRY_INFO.TOTAL_COUNTRIES; j++)
				{
					if(tegCountries[j].cardOwner == -1)
						availableCards++;
				}
				
				cardStack = new int[availableCards];
				
				if(random==null)
				{
					random = new Random(System.currentTimeMillis());
				}
			
				for(int i = 0; i < availableCards; i++)
				{			
					do
					{
						cardInStack = false;
					
						do
						{
							card = random.nextInt() % COUNTRY_INFO.TOTAL_COUNTRIES;
						}
						while(card < 0);
						
						if(i > 0)
						{
							for(int j = i - 1; j >= 0; j--)
							{
								if(card == cardStack[j])
								{
									cardInStack = true;
									break;
								}
							}
						}
					}
					while(cardInStack || tegCountries[card].cardOwner != -1);
					
					cardStack[i] = card;
				}
				
				stackIndex = availableCards - 1;
				
				loadingStep++;
				
			break;
			
			case INGAME.LOADING_IMAGES:
				
				cameraX = INGAME.VIEWPORT_X;
				cameraY = INGAME.VIEWPORT_Y;
				cursorX = INGAME.VIEWPORT_X + INGAME.VIEWPORT_W / 2;
				cursorY = INGAME.VIEWPORT_Y + INGAME.VIEWPORT_H / 2;
				
				try
				{
					cursorImg = Image.createImage(SPRITES.CURSOR_IMAGE);
					diceImg = Image.createImage(SPRITES.DICE_IMAGE);
					cardsImg = Image.createImage(SPRITES.CARDS_IMAGE);
				}
				catch(Exception e)
				{
				}
				
				if(cPlayer.roundType == INGAME.ROUND_TYPE_PUT_ARMIES)
				{
					for(int i = 0; i < rules[RULES.MATCH_PLAYERS]; i++)
					{
						tegPlayers[i].armies = calcPlayerArmies(i);
					}
				}
				
				loadingStep++;
				
			break;
		}
		
		if(loadingStep < INGAME.LOADING_COMPLETE)
		{
			cGame_g.setColor(0);
			cGame_g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
			cGame_g.setColor(0xFF0000);
			cGame_g.fillRect(SCREEN_WIDTH / 2 - SCREEN_WIDTH / 4, SCREEN_HEIGHT - (SCREEN_HEIGHT / 15) * 2, (loadingStep * (SCREEN_WIDTH / 2)) / (INGAME.LOADING_COMPLETE - 1), SCREEN_HEIGHT / 15);
			cGame_g.setColor(0xFFFFFF);
			cGame_g.drawRect(SCREEN_WIDTH / 2 - SCREEN_WIDTH / 4, SCREEN_HEIGHT - (SCREEN_HEIGHT / 15) * 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 15);
		}
		else
		{
			SetIngameState(STATES.INGAME_STANDARD_ROUND);
			loadingStep = 0;
		}
	}